package org.dromara.pay.crud.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.service.pay.PayAlipayOrdersSignTranslationService;
    import org.dromara.common.mybatis.core.page.TableDataInfo;
    import org.dromara.common.mybatis.core.page.PageQuery;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.cache.annotation.Cacheable;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.pay.domain.bo.PayAlipayOrdersSignBo;
import org.dromara.pay.domain.vo.PayAlipayOrdersSignVo;
import org.dromara.pay.domain.PayAlipayOrdersSign;
import org.dromara.pay.mapper.PayAlipayOrdersSignMapper;
import org.dromara.pay.crud.service.IPayAlipayOrdersSignCrudService;
import org.dromara.pay.domain.query.PayAlipayOrdersSignQuery;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 支付宝订单信息Service业务层处理
 *
 * @author yf
 * @date 2024-07-22
 */
@RequiredArgsConstructor
@Service
public class PayAlipayOrdersSignCrudServiceImpl implements IPayAlipayOrdersSignCrudService, PayAlipayOrdersSignTranslationService {

    private final PayAlipayOrdersSignMapper baseMapper;

    /**
     * 查询支付宝订单信息
     *
     * @param alipayId 主键
     * @return 支付宝订单信息
     */
    @Override
    public PayAlipayOrdersSignVo queryById(String alipayId){
        return baseMapper.selectVoById(alipayId);
    }


    /**
     * 分页查询支付宝订单信息列表
     *
     * @param query        查询条件
     * @param pageQuery 分页参数
     * @return 支付宝订单信息分页列表
     */
     @Override
     public TableDataInfo<PayAlipayOrdersSignVo> queryPageList(PayAlipayOrdersSignQuery query, PageQuery pageQuery) {
        LambdaQueryWrapper<PayAlipayOrdersSign> lqw = buildQueryWrapper(query);
        Page<PayAlipayOrdersSignVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
     }


    /**
     * 查询符合条件的支付宝订单信息列表
     *
     * @param query 查询条件
     * @return 支付宝订单信息列表
     */
    @Override
    public List<PayAlipayOrdersSignVo> queryList(PayAlipayOrdersSignQuery query) {
        LambdaQueryWrapper<PayAlipayOrdersSign> lqw = buildQueryWrapper(query);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayAlipayOrdersSign> buildQueryWrapper(PayAlipayOrdersSignQuery query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<PayAlipayOrdersSign> lqw = Wrappers.lambdaQuery();
                    lqw.eq(StringUtils.isNotBlank(query.getAlipayNo()), PayAlipayOrdersSign::getAlipayNo, query.getAlipayNo());
                    lqw.eq(StringUtils.isNotBlank(query.getSubject()), PayAlipayOrdersSign::getSubject, query.getSubject());
                    lqw.eq(StringUtils.isNotBlank(query.getTradeStatus()), PayAlipayOrdersSign::getTradeStatus, query.getTradeStatus());
                    lqw.eq(query.getTotalAmount() != null, PayAlipayOrdersSign::getTotalAmount, query.getTotalAmount());
                    lqw.eq(StringUtils.isNotBlank(query.getBuyer()), PayAlipayOrdersSign::getBuyer, query.getBuyer());
                    lqw.between(params.get("beginGmtPayment") != null && params.get("endGmtPayment") != null,
                        PayAlipayOrdersSign::getGmtPayment ,params.get("beginGmtPayment"), params.get("endGmtPayment"));
                    lqw.eq(query.getBuyerPayAmount() != null, PayAlipayOrdersSign::getBuyerPayAmount, query.getBuyerPayAmount());
                    lqw.between(params.get("beginPayTime") != null && params.get("endPayTime") != null,
                        PayAlipayOrdersSign::getPayTime ,params.get("beginPayTime"), params.get("endPayTime"));
                    lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
                        PayAlipayOrdersSign::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
                    lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
                        PayAlipayOrdersSign::getUpdateTime ,params.get("beginUpdateTime"), params.get("endUpdateTime"));
        return lqw;
    }



    /**
     * 新增支付宝订单信息
     *
     * @param bo 支付宝订单信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PayAlipayOrdersSignBo bo) {
        PayAlipayOrdersSign add = MapstructUtils.convert(bo, PayAlipayOrdersSign.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAlipayId(add.getAlipayId());
        }
        return flag;
    }

    /**
     * 修改支付宝订单信息
     *
     * @param bo 支付宝订单信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PayAlipayOrdersSignBo bo) {
        PayAlipayOrdersSign update = MapstructUtils.convert(bo, PayAlipayOrdersSign.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PayAlipayOrdersSign entity){
        //SAIL 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除支付宝订单信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //SAIL 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 翻译支付宝订单信息信息
     *
     * @param id 主键
     * @return ${vo}
     */
    @Override
    @Cacheable(cacheNames = PayAlipayOrdersSign.TRANS_ID_TO_OBJECT, key = "#id")
    public Object selectObjectById(Integer id){
        return baseMapper.selectVoById(id);
    }
}
